import { createApp } from 'vue';
import Message from './Message.vue';

// 定义类型
export type MessageType = 'success' | 'error' | 'default';
// 实例化方法
const createMessage = (message: string, type: MessageType, timeout = 2000) => {
  // 实例化组件
  const messageInstance = createApp(Message, { message, type });
  // 创建组件挂载的节点
  const mountNode = document.createElement('div');
  document.body.appendChild(mountNode);
  messageInstance.mount(mountNode);
  setTimeout(() => {
    // 默认2秒超时后，卸载组件并删除节点
    messageInstance.unmount();
    document.body.removeChild(mountNode);
  }, timeout);
};

export default createMessage;
